﻿using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing;
using Microsoft.SharePoint.Client.Publishing.Navigation;
using Microsoft.SharePoint.Client.Taxonomy;
using System;
using System.Net;

namespace Office365.SharePointOnline.Cmdlets
{
    [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Set, "NavigationSettings")]
    public class Set_StructuralNavigation : System.Management.Automation.PSCmdlet
    {
        [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
        public string Url;

        [System.Management.Automation.Parameter(Position = 1, Mandatory = false)]
        public string User;

        [System.Management.Automation.Parameter(Position = 2, Mandatory = false)]
        public string Password;

        [System.Management.Automation.Parameter(Position = 3, Mandatory = false)]
        public string AuthenticationMethod;

        [System.Management.Automation.Parameter(Position = 3, Mandatory = true)]
        public string NavigationSetting;

        // use only if onpremise authentication
        [System.Management.Automation.Parameter(Position = 4, Mandatory = false)]
        public string Domain;

        protected override void ProcessRecord()
        {
            // check parameters
            ClientContext context = new ClientContext(Url);
            ICredentials credentials;
            Web web = context.Web;
            this.WriteVerbose("Logging on.");
            if (string.IsNullOrEmpty(Url))
            {
                this.WriteObject("Error: url is not set");
            }
            else
            {
                if (!string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(User))
                {
                    AuthenticationHelper authenticationHelper = new AuthenticationHelper() { AuthenticationMethod = AuthenticationMethod, Domain = Domain, Password = Password, User = User };
                    credentials = authenticationHelper.GetCredentials();
                    if (credentials != null)
                    {
                        context.Credentials = credentials as ICredentials;
                    }
                    else
                    {
                        throw new NullReferenceException("No credentials returned.");
                    }
                }
                else
                {
                    this.WriteVerbose("Reusing existing credentials");
                }

                this.WriteVerbose("Setting the navigation to structural.");
                context.Load(web);
                context.ExecuteQuery();

                SetNavigationSettingsProperties(context, web);
            }
        }

        private void SetNavigationSettingsProperties(ClientContext context, Web web)
        {
            this.WriteVerbose("Setting publishing navigation");
            context.Load(web);
            context.ExecuteQuery();
            PublishingWeb pWeb = Microsoft.SharePoint.Client.Publishing.PublishingWeb.GetPublishingWeb(context, web);
            WebNavigationSettings nav = new WebNavigationSettings(context, web);
            TaxonomySession tx = Microsoft.SharePoint.Client.Taxonomy.TaxonomySession.GetTaxonomySession(context);
            context.Load(tx);
            context.Load(nav.GlobalNavigation);
            context.ExecuteQuery();
            switch (NavigationSetting)
            {
                case "InheritFromParentWeb":
                    nav.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
                    this.WriteObject("Global Navigation is set to inherit from parentweb.");
                    break;
                case "PortalProvider":
                    nav.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
                    this.WriteObject("Global Navigation is set to show underlying.");
                    break;
                case "TaxonomyProvider":
                    nav.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                    this.WriteObject("Global Navigation is set to a taxonomy.");
                    break;
                case "Unknown":
                    nav.GlobalNavigation.Source = StandardNavigationSource.Unknown;
                    this.WriteObject("Global Navigation is set to unknown.");
                    break;
                default:
                    throw new ArgumentOutOfRangeException("Unknown parameter for NavigationSetting. Valid values are InheritFromParentWeb, PortalProvider, TaxonomyProvider or Unknown.");
            }
        
            nav.Update(tx);
            context.ExecuteQuery();
        }
    }
}
